package com.arsn.myf.system.service.impl;

import com.arsn.myf.common.node.MenuTreeNode;
import com.arsn.myf.common.node.TreeNode;
import com.arsn.myf.common.util.auth.AuthUtil;
import com.arsn.myf.common.util.node.NodeTreeUtil;
import com.arsn.myf.system.entity.Menu;
import com.arsn.myf.system.entity.User;
import com.arsn.myf.system.mapper.MenuMapper;
import com.arsn.myf.system.service.MenuService;
import com.arsn.myf.system.vo.MenuVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/**
 * 菜单实现层
 * @author zhongrj
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * 自定义查询菜单分页数据
     * @param menu
     * @return
     */
    @Override
    public List<MenuVO> selectPageMenuList(MenuVO menu) {
        return NodeTreeUtil.getMenuNodeTree(baseMapper.selectPageMenuList(menu));
    }

    /**
     * 登录完成获取菜单列表
     * @param menu 菜单实体
     * @return
     */
    @Override
    public List<MenuTreeNode> getMenu(MenuVO menu) {
        User user = AuthUtil.getUser().getUser();
        //根据角色查询对应的菜单
        //如果是管理员 admin,则包含所有的菜单
        if (user.getRoleId().equals("1")){
            //查询所有菜单
            return NodeTreeUtil.getSystemNodeTree(baseMapper.getMenu(null,menu));
        }
        return NodeTreeUtil.getSystemNodeTree(baseMapper.getMenu(Arrays.asList(user.getRoleId().split(",")),menu));
    }

    /**
     * 获取菜单树
     * @param menu 菜单查询参数对象
     * @return
     */
    @Override
    public List<TreeNode> getMenuTree(MenuVO menu) {
        return NodeTreeUtil.getNodeTree(baseMapper.getMenuTree(menu));
    }

    /**
     * 根据角色id 查询当前角色拥有的菜单集合id
     * @param roleId 角色id
     * @return
     */
    @Override
    public List<String> getMenuListByRoleId(String roleId) {
        return baseMapper.getMenuListByRoleId(roleId);
    }
}
